package com.wslds.base.util;

import java.io.PrintWriter;
import java.io.StringWriter;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Random;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

import com.sun.syndication.feed.synd.SyndContent;
import com.sun.syndication.feed.synd.SyndContentImpl;
import com.sun.syndication.feed.synd.SyndEntry;
import com.sun.syndication.feed.synd.SyndEntryImpl;
import com.sun.syndication.feed.synd.SyndFeed;
import com.sun.syndication.feed.synd.SyndFeedImpl;
import com.wslds.bean.Article;
import com.wslds.bean.ext.ArticleExt;

public class Strings {
	
	public static boolean isNull( String s ){
		return null == s || s.trim().length() == 0 ;
	}
	
	public static boolean notNull( String s ){
		return !Strings.isNull(s);
	}
	
	public static String cutPageQueryString( String queryString ){
		return queryString.replaceAll("(&{0,1})page=(\\d{1,10})", "");
	}
	
	public static String getTrace(Throwable t) {
        StringWriter stringWriter= new StringWriter();
        PrintWriter writer= new PrintWriter(stringWriter);
        t.printStackTrace(writer);
        StringBuffer buffer= stringWriter.getBuffer();
        return buffer.toString();
    }
	
	public static Map<String,String> genRandomQuestion(){
		Map<String,String> questions = new HashMap<String,String>();
		questions.put("'1+2=?'", "3");
		questions.put("'2+2=?'", "4");
		questions.put("'2*3=?'", "6");
//		questions.put("'这里的域名是?'", "'wslds.com'");
		questions.put("'23*3=?'", "69");
		questions.put("'12*3=?'", "36");
		questions.put("'7*4=?'", "28");
		questions.put("'9*3=?'", "27");
		questions.put("'5*9=?'", "45");
		questions.put("'9*2=?'", "18");
		return questions;
	}
	
	public static SyndFeed genRss(List<ArticleExt> artList) {
		// 构造一个SyndFeed新对象
		SyndFeed feed = new SyndFeedImpl();
		// 设置Feed类型
		feed.setFeedType("rss_2.0");
		// 设置Feed基本信息
		feed.setTitle("Wslds");
		feed.setLink("http://wslds.com");
		feed.setDescription("没有伞,一样走出美好的人生");
		feed.setUri("http://wslds.com");
		feed.setCopyright("Copyright 2011-2012, Wslds.com");
		
		// 构造Feed items列表
		List<SyndEntry> entries = new ArrayList<SyndEntry>();
		
		for( Article art : artList ){
			SyndEntry entry = new SyndEntryImpl();
			entry.setLink("http://wslds.com/blog/" + art.getId() + ".html");
			entry.setTitle(art.getTitle());
			entry.setPublishedDate(art.getCreateDate());
			entry.setUri("http://wslds.com/blog/" + art.getId() + ".html");
			
			SyndContent description = new SyndContentImpl();
			description.setType("text/plain");
			description.setValue(art.getContent());
			entry.setDescription(description);
			
			entries.add(entry);
		}
		feed.setEntries(entries);
		return feed;
	}
	
	public static String genRandomName(){
		String[] randoms = {"零","一","二","三","四","五","六","七","八","九","十","瓜","蛋"};
		String[] randomCalls = {"哥","姐","弟","妹","兄","爷","师姐","师弟","师妹","师兄","师爷","师祖"};
		Random random = new Random();
		return randoms[random.nextInt(13)] + randomCalls[random.nextInt(12)];
	}
	
	public static String tranSmile( String data ){
//		Pattern p = Pattern.compile("：(\\d{1,2}):");
//		[1-9]{1}||1[0-9]{1}||20
		Pattern p = Pattern.compile("：([1-9]{1}||1[0-9]{1}||20):");
		Matcher m = p.matcher(data);
		while( m.find() ){
			 String index = m.group(1);
			 data = m.replaceFirst( BlogCons.SMILE.SMILE_START + index + BlogCons.SMILE.SMILE_END);
			 m = p.matcher(data);
		}
		return  data;
	}
	
	public static void main(String[] args) {
		cutPageQueryString("page=2&articleExt.typeId=1&page=1");
		cutPageQueryString("page=2&articleExt.typeId=1&page=1&t=2");
	}
}
